package com.titaniume.java;

import org.junit.Test;

import java.util.*;

/**
 *@program: java8
 *@description:
 *@author: renfei.huang
 *@create: 2021-10-28 10:58
 */
public class TestLambad {

    //原来的匿名内部类
    @Test
    public void test1() {
        //Comparator 定制排序
        Comparator com = new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        TreeSet<Integer> ts = new TreeSet<>(com);
    }


    //lambda
    @Test
    public void test2() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        TreeSet<Integer> ts = new TreeSet<>(com);
    }


    List<Employee> employees = Arrays.asList(
            new Employee("tom", 22, 8888),
            new Employee("Jack", 23, 8964),
            new Employee("LiDa", 42, 7821),
            new Employee("JinHai", 35, 10000),
            new Employee("LiLei", 50, 15000),
            new Employee("YeXuan", 25, 30000),
            new Employee("HeLei", 15, 2350),
            new Employee("ZhoubaiTong", 14, 1500)
    );


    public List<Employee> filterEmployes(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee e : list) {
            if (e.getAge() >= 35) {
                emps.add(e);
            }
        }
        return emps;
    }

    //需求：获取当前公司中员工年龄大于35的员工信息
    @Test
    public void test3() {

        List<Employee> employees = filterEmployes(this.employees);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    //需求： 获取当前公司中员工工资大于5000的员工信息
    public List<Employee> filterEmployes2(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee e : list) {
            if (e.getSalary() >= 500) {
                emps.add(e);
            }
        }
        return emps;
    }

    //优化方式一： 策略设计模式
    @Test
    public void test4() {
        List<Employee> list1 = filterEmployee(this.employees, new FilterEmployeeByAge());
        for (Employee employee : list1) {
            System.out.println(employee);
        }
        System.out.println("-------------------");
        List<Employee> list2 = filterEmployee(this.employees, new FilterEmployeeBySalary());
        for (Employee employee : list2) {
            System.out.println(employee);
        }

    }

    public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> myPredicate) {
        List<Employee> emps = new ArrayList<>();
        for (Employee employee : list) {
            if (myPredicate.test(employee)) {
                emps.add(employee);
            }
        }
        return emps;
    }

    //优化方式二：匿名内部类
    @Test
    public void test5() {
        List<Employee> employees = filterEmployee(this.employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() <= 5000;
            }
        });
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    //优化方式三： Lambda 表达式
    @Test
    public  void test6(){

        List<Employee> employees = filterEmployee(this.employees, (e) -> e.getSalary() <= 5000);

        employees.forEach(System.out::println);

    }


    //优化方式四：Stream Api
    @Test
    public  void test7(){
        employees.stream()
                    .filter((e) -> e.getSalary() <=5000)
                    .limit(1)
                    .forEach(System.out::println);

        System.out.println("-------------------");


        employees.stream().map(Employee::getName)
                .forEach(System.out::println);

    }



}
